package com.ailight.smart.api.factory;

import com.ailight.smart.api.enums.ModelEnums;
import com.ailight.smart.api.strategy.LightOperationStrategy;
import lombok.NonNull;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

@Component
public class ModelFactory implements ApplicationContextAware {
    private static final Map<String, LightOperationStrategy> STRATEGY_MAP = new ConcurrentHashMap<>();


    @Override
    public void setApplicationContext(@NonNull ApplicationContext applicationContext) throws BeansException {
        for (ModelEnums modelEnum : ModelEnums.values()) {
            STRATEGY_MAP.put(modelEnum.getModel(), (LightOperationStrategy) applicationContext.getBean(modelEnum.getStrategy()));
        }
    }

    public LightOperationStrategy getStrategy(ModelEnums model) {
        return STRATEGY_MAP.get(model.getModel());
    }
}
